package com.dyc.caradvertisting.ui.fmt.register;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.dyc.caradvertisting.R;
import com.dyc.caradvertisting.bean.req.Register;
import com.dyc.caradvertisting.comm.GsonUtils;
import com.njh.tools.base.BaseFragment;
import com.njh.tools.common.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegisterFmt extends BaseFragment<RegisterPresenter, RegisterModel> implements RegisterContract.View {


    @BindView(R.id.edt_vcode)
    EditText edtVcode;
    @BindView(R.id.edt_pwd)
    EditText edtPwd;
    @BindView(R.id.btn_send_message)
    Button btnSendMessage;

    public String phone;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_send_code)
    Button tvSendCode;

    public RegisterFmt() {
        // Required empty public constructor
    }

    public static RegisterFmt newInstance() {
        RegisterFmt fragment = new RegisterFmt();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return getView(inflater, container);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_register_fmt;
    }

    @Override
    public void initView(View baseView) {
        ButterKnife.bind(this, baseView);
        initToolbar(R.string.txt_message);
    }

    @Override
    public boolean isSwipeBack() {
        return false;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel, this._mActivity);
    }

    @Override
    public void finish() {
        this._mActivity.onBackPressedSupport();
    }

    @OnClick(R.id.btn_send_message)
    public void onClick() {
        String phone=etPhone.getText().toString();
        String Vcode = edtVcode.getText().toString();
        String pwd = edtPwd.getText().toString();
        if (TextUtils.isEmpty(phone)){
            ToastUtils.showLong(this.getContext(),"请输入手机号");
            return;
        }
        if (phone.length()!=11){
            ToastUtils.showLong(this.getContext(),"手机号输入有误");
            return;
        }
        if (TextUtils.isEmpty(Vcode)){
            ToastUtils.showLong(this.getContext(),"请获取短信验证码");
            return;
        }
        if (TextUtils.isEmpty(pwd)){
            ToastUtils.showLong(this.getContext(),"请输入密码");
            return;
        }
        if (6>pwd.length()){
            ToastUtils.showLong(this.getContext(),"密码长度不能小于6位");
            return;
        }
        if (16<pwd.length()){
            ToastUtils.showLong(this.getContext(),"密码长度不能大于16位");
            return;
        }
        Register register = new Register();
        register.setPhone(phone);
        register.setPsw(pwd);
        register.setVcode(Vcode);
        mPresenter.reqUserRegister(GsonUtils.getmGson().toJson(register));
    }
}
